package com.blog.dao.frontpage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.blog.dao.conn.BaseDao;
import com.blog.dao.conn.ParamSet;
import com.blog.dao.conn.RowDatas;
import com.blog.entity.ArticleConstraintEntity;
import com.blog.entity.ArticleEntity;
import com.blog.entity.TypesEntity;
/**
 * 前台首页所有文章
 * @author Administrator
 *
 */
public class ArticleDao extends BaseDao{

private RowDatas<ArticleConstraintEntity> articleInfo = new RowDatas<ArticleConstraintEntity>() {
		
		@Override
		public ArticleConstraintEntity getRowData(ResultSet rs) throws SQLException {
			ArticleConstraintEntity ace = new ArticleConstraintEntity();
			ace.setArticle_title(rs.getString("article_title"));
			ace.setArticle_time(rs.getTimestamp("article_time"));
			ace.setArticle_readnumber(rs.getInt("article_readnumber"));
			ace.setWriter_id(rs.getInt("writer_id"));
			ace.setArticle_id(rs.getInt("article_id"));
			ace.setArticle_content(rs.getString("article_content"));
			ace.setArticle_supportnumber(rs.getInt("article_supportnumber"));
//			ace.setArticle_status(rs.getInt("article_status"));
			ace.setUser_name(rs.getString("user_name"));
			ace.setUser_image(rs.getString("user_image"));
			ace.setTypes_name(rs.getString("types_name"));
//			ace.setArticle_discussnumber(rs.getInt("article_discussnumber"));
			return ace;
		}
	};
	
	/**
	 * 查询首页所有分类（根据时间排序）
	 * 用户名，标题，发表时间，正文，浏览量，点击数，评论数（计算），所属分类
	 */
	public List<ArticleConstraintEntity> getAllArticleInfo(){
		String sql="select a.article_title,a.article_id,a.article_content,a.article_time,a.article_readnumber,\r\n" + 
				"a.article_supportnumber,a.writer_id,u.user_name,u.user_image,t.types_name \r\n" + 
				"from tb_article a\r\n" + 
				"LEFT JOIN tb_user u on u.user_id =a.writer_id\r\n" + 
				"LEFT JOIN tb_types t ON a.article_typesid = t.types_id "
				+ "order by a.article_time Desc";
		System.out.println(sql);
		//System.out.println(sql);
		return queryList(sql,articleInfo);
	}
	
	private RowDatas<ArticleConstraintEntity> hotRecommendInfo = new RowDatas<ArticleConstraintEntity>() {

		@Override
		public ArticleConstraintEntity getRowData(ResultSet rs) throws SQLException {
			ArticleConstraintEntity ace = new ArticleConstraintEntity();
			ace.setArticle_title(rs.getString("list"));
			ace.setTypes_name(rs.getString("types_name"));
			return ace;
		}
	};
	/**
	 * 查询首页的热门推荐
	 * 热门分类，热门文章标题
	 */
	public List<ArticleConstraintEntity> getHotRecommend(){
		String sql = "SELECT t.types_name,GROUP_CONCAT(a.article_title) list from tb_types t\r\n" + 
				"LEFT JOIN tb_article a on a.article_typesid = t.types_id GROUP BY types_name";
		return queryList(sql, hotRecommendInfo);
		
	}
	
	/**
	 * 根据类别查询文章详情
	 */
	public List<ArticleConstraintEntity> getArticleByTypes(String typenames ){
		String sql = "select a.article_title,a.article_id,a.article_content,a.article_time,a.article_readnumber,\r\n" + 
				"a.article_supportnumber,a.writer_id,u.user_name,u.user_image,t.types_name \r\n" + 
				"from tb_article a\r\n" + 
				"LEFT JOIN tb_user u on u.user_id =a.writer_id\r\n" + 
				"LEFT JOIN tb_types t ON a.article_typesid = t.types_id\r\n" + 
				"where types_name=?";
		return queryList(sql,new ParamSet(typenames),articleInfo);
		
	}
	
	/**
	 * 根据标题查询文章详情
	 */
	public ArticleConstraintEntity getArticleByTitle(String title){
		String sql = "select a.article_title,a.article_id,a.article_content,a.article_time,a.article_readnumber,\r\n" + 
				"a.article_supportnumber,a.writer_id,u.user_name,u.user_image,t.types_name \r\n" + 
				"from tb_article a\r\n" + 
				"LEFT JOIN tb_user u on u.user_id =a.writer_id\r\n" + 
				"LEFT JOIN tb_types t ON a.article_typesid = t.types_id\r\n" + 
				"where a.article_id=?";
		return query(sql,new ParamSet(title),articleInfo);
		
	}
	
	/**
	 *根据文章id修改浏览量
	 */
	public int updateReadNumber(int articleid) {
		int row = 0;
		String sql = "update tb_article set article_readnumber =article_readnumber+1 where article_id = ?";
		row = updateExecuted(sql, new ParamSet(articleid));
		return row;
	}

	
}
